<?php
require_once APPLICATION_PATH . '/../library/AlibabaSdk/AlibabaSdk.php';
class Uuch_Reader_AlibabaReader extends Uuch_Reader_ProductReader {
    public function getInfo($url) {
        $urlInfo = pathinfo($url);
        $basename = explode(".", $urlInfo["basename"]);
        $productId = $basename[0];
        $alibaba = new AlibabaSdk();
        $result = $alibaba->getProduct($productId);
        $productInfo = $result->getProductInfo();
        $imgArr = $productInfo->getImage()->getImages();
        $saleInfo = $productInfo->getSaleInfo();
        $priceRanages = $saleInfo->getPriceRanges();
        $product = new Application_Model_Product();
        $product->Name = $productInfo->getSubject();
        $product->Price = (float)$priceRanages[0]->getPrice();
        $product->Sku = $productId;
        $product->Url = $url;
        $product->ShipFee = 0;
        $product->Quantity = 999;
        $product->Image = "https://cbu01.alicdn.com/".$imgArr[0];
        $product->RemoteImage = $product->Image;
        return $product;
    }
    public function getInfoForNew($url) {
		
        $urlInfo = pathinfo($url);
        $basename = explode(".", $urlInfo["basename"]);
		
        $productId = $basename[0];
		
        $alibaba = new AlibabaSdk();
		echo 'PID:'.$productId;
        $result = $alibaba->getProduct($productId);
		echo 'TEST';
        $productInfo = $result->getProductInfo();
        $imgArr = $productInfo->getImage()->getImages();
        $saleInfo = $productInfo->getSaleInfo();
        $priceRanages = $saleInfo->getPriceRanges();
        $product = new Application_Model_Product();
        $product->Name = $productInfo->getSubject();
        $product->Price = (float)$priceRanages[0]->getPrice();
        $product->Sku = $productId;
        $product->Url = $url;
        $product->ShipFee = 0;
        $product->Quantity = 999;
        $product->Image = "https://cbu01.alicdn.com/".$imgArr[0];
        $product->RemoteImage = $product->Image;
        $shop = Application_Model_Shop::getByName($product->Name);
        if (NULL == $shop){
            $shop = new Application_Model_Shop();
            $shop->Name = $product->Name;
            $shop->IsTaobao = True;
            $shop->Url = $url;
            $shop->save();
        }
        $product->ShopId = $shop->Id;
        
        $product->save();
        return array(
                'product' => $product, 
                'shopName' => strip_tags($product->Name),
                'siteName' => '阿里巴巴',
                'shopUrl' => $url,
                'url' => $url,
                'siteUrl' => $url
        );
    }
}
